<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

$sql = "SELECT  promise_date ,  card_id1 ,  reason ,  total ,  remain , tbl_pet_budget.pet_id, return_date"
        . ", remain_cash, return_amount "
        . " FROM tbl_promise_budget "
        . " JOIN tbl_pet_budget ON tbl_promise_budget.id = tbl_pet_budget.promise_id "
        . " JOIN tbl_return ON tbl_promise_budget.id = tbl_return.promise_id "
        . " UNION"
        . " SELECT  promise_date ,  card_id AS card_id1 , objective AS reason , borrow_amount AS total "
        . " , remain , tbl_pet_borrow.pet_id, return_date"
        . ", remain_cash, return_amount "
        . " FROM tbl_promise_borrow "
        . " JOIN tbl_pet_borrow ON tbl_promise_borrow.borrow_id = tbl_pet_borrow.borrow_id "
        . " JOIN tbl_return ON tbl_promise_borrow.borrow_id = tbl_return.borrow_id ";

echo $sql;

$db = NEW myDBS();
$db->selectDB("account");

$result = $db->runSQL($sql);
?>
<script type="text/javascript">
    $(document).ready(function() {
        $('#petition').dataTable();
    });
</script>
<table id="petition" class="table table-bordered table-hover" width="100%" >
    <caption><br></caption>
    <thead>
        <tr>
            <th>เลขที่ฎีกา</th>
            <th>วันที่ยืม</th>
            <th>นามผู้ยืม</th>
            <th>ยืมเพื่อใช้ราชการ</th>
            <th>จำนวน</th>
            <th>วันที่ส่งใช้เงินยืม</th>
            <th>หลักฐาน</th>
            <th>ตัวเงิน</th>
            <th>คงค้างเป็นเงิน</th>
        </tr>
    </thead>
    <tbody>


        <?php
        while ($rows = $db->fetch_object($result)) {
            //echo $rows->pet_id;
            ?>
            <tr>
                <td><?php echo $rows->pet_id; ?></td>
                <td><?php echo $rows->promise_date; ?></td>
                <td><?php echo get_name_person($rows->card_id1); ?></td>
                <td><?php echo $rows->reason; ?></td>
                <td><p class="text-right"><?php echo number_format($rows->total, 2); ?></p></td>
                <td><?php echo $rows->return_date; ?></td>
                <td><p class="text-right"><?php echo number_format($rows->return_amount, 2); ?></p></td>
                <td>
                    <p class="text-right">
                        <?php
                        if ($rows->remain_cash == "on") {
                            echo number_format($rows->total - $rows->return_amount, 2);
                        }
                        //echo $rows->return_amount;
                        ?>
                    </p>
                </td>
                <td><p class="text-right"><?php echo number_format($rows->remain, 2); ?></p></td>
            </tr>
            <?php
        }
        ?>

    </tbody>
</table>
<a href="javascript:print()" class="btn btn-info">พิมพ์รายงาน</a>